package com.jinxinol.core.util.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.rmi.UnknownHostException;

/* 
 * Copyright (C) 2011 金鑫电脑公司. 
 * 
 * 本系统是商用软件,未经授权擅自复制或传播本程序的部分或全部将是非法的. 
 * 
 * $$Id:00084 
 * Date 2011年11月11日 Author 李彦 Description 用于发送socket命令
 */

public class SendCommand {
	private static byte[] lockObj=new byte[0];
	private static byte[] lockObj212=new byte[0];
	private static byte[] lockObj215=new byte[0];
	private static byte[] lockObj217=new byte[0];
	private static byte[] lockObj223=new byte[0];
	private static byte[] lockObj225=new byte[0];
	private static byte[] lockObj228=new byte[0];
	@SuppressWarnings("unused")
	private static int i=0;

	/**
	 * description 发送socket命令到终端控制
	 *
	 * @param url 访问地址
	 * @param port 端口号
	 * @param command 命令信息
	 *
	 * @return result 终端回复信息
	 */

	public static void main(String args[]){
		Socket socket = null;
		DataOutputStream doc = null;
		String command = "lighton";
		try {
			socket = new Socket("localhost",7548);
			OutputStream netOut = socket.getOutputStream();
			doc = new DataOutputStream(netOut);
			doc.write(command.getBytes());
			doc.close();
			doc=null;
			socket.close();
			socket=null;
		} catch (java.net.UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String sendCommand(String url, int port, String command) {
		String result = "";
		Socket socket = null;
		DataOutputStream doc = null;
		DataInputStream in = null;
		try {
			System.out.println("url:" + url + ",port:" + port + ",command:"
					+ command);
			socket = new Socket(url, port);
//			System.out.println("url:"+url+);
			//向服务器端发送字符串      
			OutputStream netOut = socket.getOutputStream();
			doc = new DataOutputStream(netOut);
			in = new DataInputStream(socket.getInputStream());
			doc.write(command.getBytes());
					
			byte[] tt=new byte[10];
			in.read(tt);
			result = new String(tt,"utf-8");
			char rr;
			String ssr;
			int inde=0;
            for(int i=1;i<result.length();i++){
            	rr=result.charAt(i);
            	ssr=rr+"";
            	if((ssr.equals("0"))||(ssr.equals("1"))||(ssr.equals("2"))||(ssr.equals("3"))||(ssr.equals("4"))||(ssr.equals("5"))||(ssr.equals("6"))||(ssr.equals("7"))||(ssr.equals("8"))||(ssr.equals("9"))){
            	}
            	else{
            		inde=i;
            		break;
            	}
            }
			result=result.substring(0, inde);
			doc.close();
			doc=null;
			in.close();
			in=null;
			socket.close();
			socket=null;

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null)
					in.close();
				if (doc != null)
					doc.close();
				if (socket != null)
					socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * description 发送socket命令到终端控制
	 *
	 * @param url 访问地址
	 * @param port 端口号
	 * @param command 命令信息
	 *
	 * @return result 终端回复信息
	 */
	public static String sendCommandY(String url, int port, String command) {
		
		synchronized(lockObj){
			i++;
			//System.out.println(i+"执行同步方法开始时间111111："+System.currentTimeMillis());
			String result = "";
			Socket socket = null;
			DataOutputStream doc = null;
			DataInputStream in = null;
//			System.out.println("#$#$%#$^&%*&*%^%#$%^&(*&"+url+"====="+port);
			try {
				socket = new Socket(url, port);
//				System.out.println(socket);
				//向服务器端发送字符串      
				OutputStream netOut = socket.getOutputStream();
				doc = new DataOutputStream(netOut);
				in = new DataInputStream(socket.getInputStream());
				//			System.out.println("url:" + url + ",port:" + port + ",command:"
				//					+ command);
				//System.out.println("1打算向单片机传送数据");
				doc.write(command.getBytes());
				//System.out.println("2向单片机传送数据完成");
				//			System.out.println("ok?");
				//System.out.println("3等待单片机的数据");
				byte res = in.readByte();
				//System.out.println("4收到单片机的数据");
				byte[] info = new byte[] { res };
//				System.out.println("input====" + new String(info));
				result = new String(info);
	
				doc.close();
				doc=null;
				in.close();
				in=null;
				socket.close();
				socket=null;
	
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (in != null)
						in.close();
					if (doc != null)
						doc.close();
					if (socket != null)
						socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(i+"执行同步方法结束时间222222："+System.currentTimeMillis());
			return result;
		}
		
	}
	
	public static String sendCommand212(String url, int port, String command) {
		
		synchronized(lockObj212){
			i++;
			//System.out.println(i+"执行212同步方法开始时间111111："+System.currentTimeMillis());
			String result = "";
			Socket socket = null;
			DataOutputStream doc = null;
			DataInputStream in = null;
//			System.out.println("#$#$%#$^&%*&*%^%#$%^&(*&"+url+"====="+port);
			try {
				socket = new Socket(url, port);
//				System.out.println(socket);
				//向服务器端发送字符串      
				OutputStream netOut = socket.getOutputStream();
				doc = new DataOutputStream(netOut);
				in = new DataInputStream(socket.getInputStream());
				//			System.out.println("url:" + url + ",port:" + port + ",command:"
				//					+ command);
				//System.out.println("1打算向单片机传送数据");
				doc.write(command.getBytes());
				//System.out.println("2向单片机传送数据完成");
				//			System.out.println("ok?");
				//System.out.println("3等待单片机的数据");
				byte res = in.readByte();
				//System.out.println("4收到单片机的数据");
				byte[] info = new byte[] { res };
//				System.out.println("input====" + new String(info));
				result = new String(info);
	
				doc.close();
				doc=null;
				in.close();
				in=null;
				socket.close();
				socket=null;
	
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (in != null)
						in.close();
					if (doc != null)
						doc.close();
					if (socket != null)
						socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(i+"执行212同步方法结束时间222222："+System.currentTimeMillis());
			return result;
		}
		
	}

	public static String sendCommand215(String url, int port, String command) {
		
		synchronized(lockObj215){
			i++;
			//System.out.println(i+"执行215同步方法开始时间111111："+System.currentTimeMillis());
			String result = "";
			Socket socket = null;
			DataOutputStream doc = null;
			DataInputStream in = null;
//			System.out.println("#$#$%#$^&%*&*%^%#$%^&(*&"+url+"====="+port);
			try {
				socket = new Socket(url, port);
//				System.out.println(socket);
				//向服务器端发送字符串      
				OutputStream netOut = socket.getOutputStream();
				doc = new DataOutputStream(netOut);
				in = new DataInputStream(socket.getInputStream());
				//			System.out.println("url:" + url + ",port:" + port + ",command:"
				//					+ command);
				//System.out.println("1打算向单片机传送数据");
				doc.write(command.getBytes());
				//System.out.println("2向单片机传送数据完成");
				//			System.out.println("ok?");
				//System.out.println("3等待单片机的数据");
				byte res = in.readByte();
				//System.out.println("4收到单片机的数据");
				byte[] info = new byte[] { res };
//				System.out.println("input====" + new String(info));
				result = new String(info);
	
				doc.close();
				doc=null;
				in.close();
				in=null;
				socket.close();
				socket=null;
	
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (in != null)
						in.close();
					if (doc != null)
						doc.close();
					if (socket != null)
						socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(i+"执行215同步方法结束时间222222："+System.currentTimeMillis());
			return result;
		}
		
	}
    
	public static String sendCommand217(String url, int port, String command) {
		
		synchronized(lockObj217){
			i++;
			//System.out.println(i+"执行217同步方法开始时间111111："+System.currentTimeMillis());
			String result = "";
			Socket socket = null;
			DataOutputStream doc = null;
			DataInputStream in = null;
	//		System.out.println("#$#$%#$^&%*&*%^%#$%^&(*&"+url+"====="+port);
			try {
				socket = new Socket(url, port);
	//			System.out.println(socket);
				//向服务器端发送字符串      
				OutputStream netOut = socket.getOutputStream();
				doc = new DataOutputStream(netOut);
				in = new DataInputStream(socket.getInputStream());
				//			System.out.println("url:" + url + ",port:" + port + ",command:"
				//					+ command);
				//System.out.println("1打算向单片机传送数据");
				doc.write(command.getBytes());
				//System.out.println("2向单片机传送数据完成");
				//			System.out.println("ok?");
				//System.out.println("3等待单片机的数据");
				byte res = in.readByte();
				//System.out.println("4收到单片机的数据");
				byte[] info = new byte[] { res };
	//			System.out.println("input====" + new String(info));
				result = new String(info);
	
				doc.close();
				doc=null;
				in.close();
				in=null;
				socket.close();
				socket=null;
	
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (in != null)
						in.close();
					if (doc != null)
						doc.close();
					if (socket != null)
						socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(i+"执行217同步方法结束时间222222："+System.currentTimeMillis());
			return result;
		}
		
	}
	
    public static String sendCommand223(String url, int port, String command) {
		
		synchronized(lockObj223){
			i++;
			//System.out.println(i+"执行223同步方法开始时间111111："+System.currentTimeMillis());
			String result = "";
			Socket socket = null;
			DataOutputStream doc = null;
			DataInputStream in = null;
//			System.out.println("#$#$%#$^&%*&*%^%#$%^&(*&"+url+"====="+port);
			try {
				socket = new Socket(url, port);
//				System.out.println(socket);
				//向服务器端发送字符串      
				OutputStream netOut = socket.getOutputStream();
				doc = new DataOutputStream(netOut);
				in = new DataInputStream(socket.getInputStream());
				//			System.out.println("url:" + url + ",port:" + port + ",command:"
				//					+ command);
				//System.out.println("1打算向单片机传送数据");
				doc.write(command.getBytes());
				//System.out.println("2向单片机传送数据完成");
				//			System.out.println("ok?");
				//System.out.println("3等待单片机的数据");
				byte res = in.readByte();
				//System.out.println("4收到单片机的数据");
				byte[] info = new byte[] { res };
//				System.out.println("input====" + new String(info));
				result = new String(info);
	
				doc.close();
				doc=null;
				in.close();
				in=null;
				socket.close();
				socket=null;
	
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (in != null)
						in.close();
					if (doc != null)
						doc.close();
					if (socket != null)
						socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(i+"执行223同步方法结束时间222222："+System.currentTimeMillis());
			return result;
		}
		
	}
    
    public static String sendCommand225(String url, int port, String command) {
		
		synchronized(lockObj225){
			i++;
			//System.out.println(i+"执行225同步方法开始时间111111："+System.currentTimeMillis());
			String result = "";
			Socket socket = null;
			DataOutputStream doc = null;
			DataInputStream in = null;
//			System.out.println("#$#$%#$^&%*&*%^%#$%^&(*&"+url+"====="+port);
			try {
				socket = new Socket(url, port);
//				System.out.println(socket);
				//向服务器端发送字符串      
				OutputStream netOut = socket.getOutputStream();
				doc = new DataOutputStream(netOut);
				in = new DataInputStream(socket.getInputStream());
				//			System.out.println("url:" + url + ",port:" + port + ",command:"
				//					+ command);
				//System.out.println("1打算向单片机传送数据");
				doc.write(command.getBytes());
				//System.out.println("2向单片机传送数据完成");
				//			System.out.println("ok?");
				//System.out.println("3等待单片机的数据");
				byte res = in.readByte();
				//System.out.println("4收到单片机的数据");
				byte[] info = new byte[] { res };
//				System.out.println("input====" + new String(info));
				result = new String(info);
	
				doc.close();
				doc=null;
				in.close();
				in=null;
				socket.close();
				socket=null;
	
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (in != null)
						in.close();
					if (doc != null)
						doc.close();
					if (socket != null)
						socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(i+"执行225同步方法结束时间222222："+System.currentTimeMillis());
			return result;
		}
		
	}
    
    public static String sendCommand228(String url, int port, String command) {
		
		synchronized(lockObj228){
			i++;
			//System.out.println(i+"执行228同步方法开始时间111111："+System.currentTimeMillis());
			String result = "";
			Socket socket = null;
			DataOutputStream doc = null;
			DataInputStream in = null;
//			System.out.println("#$#$%#$^&%*&*%^%#$%^&(*&"+url+"====="+port);
			try {
				socket = new Socket(url, port);
//				System.out.println(socket);
				//向服务器端发送字符串      
				OutputStream netOut = socket.getOutputStream();
				doc = new DataOutputStream(netOut);
				in = new DataInputStream(socket.getInputStream());
				//			System.out.println("url:" + url + ",port:" + port + ",command:"
				//					+ command);
				//System.out.println("1打算向单片机传送数据");
				doc.write(command.getBytes());
				//System.out.println("2向单片机传送数据完成");
				//			System.out.println("ok?");
				//System.out.println("3等待单片机的数据");
				byte res = in.readByte();
				//System.out.println("4收到单片机的数据");
				byte[] info = new byte[] { res };
//				System.out.println("input====" + new String(info));
				result = new String(info);
	
				doc.close();
				doc=null;
				in.close();
				in=null;
				socket.close();
				socket=null;
	
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (in != null)
						in.close();
					if (doc != null)
						doc.close();
					if (socket != null)
						socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(i+"执行228同步方法结束时间222222："+System.currentTimeMillis());
			return result;
		}
		
	}
}
